-------------------------------------------------------------------------------
---! 内部方法
-------------------------------------------------------------------------------
local function random_treasure(config)
    local sum = 0
    for _, weight in ipairs(config.eventWelfareWeight) do
        sum = sum + weight
    end

    local rate = math.random(sum)
    for index, weight in ipairs(config.eventWelfareWeight) do repeat
        if rate > weight then
            rate = rate - weight
            break
        end

        local propId = config.eventWelfare[index]
        if not propId then
            break
        end

        local propCount = config.eventWelfareNum[index]
        if not propCount then
            break
        end

        local propTxt = config.eventWelfareWeightTxt[index]
        return propId, propCount, propTxt
    until true end
end

-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
TREASURE_D = {}

---! 打开随机宝箱
function TREASURE_D:open_random_box(player)
    local configs = IMPORT_D:query("event")
    if not configs then
        local result = {}
        result.error = 1
        player:send_packet("MSGS2COpenRandomBox", result)
        return
    end

    ---! 获取宝箱配置
    local config = configs[player:get_level()]
    if not config then
        local result = {}
        result.error = 2
        player:send_packet("MSGS2COpenRandomBox", result)
        return
    end

    local num = player:query("treasure", "num") or 0
    if num >= configs.randomNum then
        local result = {}
        result.error = 3
        player:send_packet("MSGS2COpenRandomBox", result)
    end

    local ti = player:query("treasure", "time")
    if ti and os.time() - ti < configs.randomTime then
        local result = {}
        result.error = 4
        player:send_packet("MSGS2COpenRandomBox", result)
        return
    end

    ---! 记录当前时间
    player:set("treasure", "time", os.time())

    local prop = player:query("treasure", "prop") or {}
    if not prop.propId or not prop.propCount then
        local propId, propCount, propTxt = random_treasure(config)
        if not propId or not propCount then
            local result = {}
            result.error = 5
            player:send_packet("MSGS2COpenRandomBox", result)
            return
        end

        ---! 记录奖励信息
        prop = player:set("treasure", "prop", { propId = propId, propCount = propCount, propTxt = propTxt })
    end

    ---! 通知客户端
    local result = {}
    result.error = 0
    result.prop = { propId = prop.propId, propCount = INVENTORY_D:get_currency_show_count(player, prop.propId, prop.propCount), }
    result.text = prop.propTxt or 0
    player:send_packet("MSGS2COpenRandomBox", result)
end

---! 领取随机宝箱
function TREASURE_D:draw_random_box(player, isdouble)
    local configs = IMPORT_D:query("event")
    if not configs then
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CDrawRandomBox", result)
        return
    end

    local prop = player:query("treasure", "prop")
    if not prop then
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CDrawRandomBox", result)
        return
    end

    ---! 清理奖励信息
    player:delete("treasure", "prop")

    local num = player:query("treasure", "num") or 0
    if num >= configs.randomNum then
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CDrawRandomBox", result)
    end

    ---! 累加奖励次数
    player:set("treasure", "num", num + 1)

    ---! 发送宝箱奖励
    if isdouble ~= 0 then
        prop.propCount = prop.propCount * 2
    end
    player:add_currency(prop.propId, prop.propCount)
    ITEM_D:send_currency_info(player)

    ---! 通知客户端
    local result = {}
    result.error = 0
    result.prop = { propId = prop.propId, propCount = INVENTORY_D:get_currency_show_count(player, prop.propId, prop.propCount), }
    result.isDouble = isdouble
    player:send_packet("MSGS2CDrawRandomBox", result)
end
